package ouc.tl.ISAP;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.util.Log;


public class SocketCommunication extends BSActivity{
	//String name,password;
	
	Socket socket=null;
	private BufferedReader in =null;
	private PrintWriter out = null;
	private InetSocketAddress isa = null;
	private static final String HOST = "192.168.7.10";
	private static final int PORT = 10000;
	private static final int TIMEOUT = 100;
	public String[] reci=null;
	SocketCommunication(){
		try {
			socket = new Socket(); 
			isa=new InetSocketAddress(HOST,PORT);
			socket.connect(isa,TIMEOUT);
			in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
			Log.i("SOCKET","starting...");
		} catch (IOException e1) {
			System.out.println("error");
			e1.printStackTrace();
		}
	}
	public int connect(){
		try{
			//socket = new Socket("192.168.7.9",10000);
			//dout= new OutputStreamWriter(socket.getOutputStream());
			//din= new InputStreamReader(socket.getInputStream());
			while(true){
				Thread.sleep(100);
				String rec = in.readLine();   
				System.out.println(rec);  
				
				reci=rec.split("\\|\\|\\|");
				for(int i=0;i<reci.length;i++)
				{
					System.out.println(reci[i]);
				}
				if(reci[0].equals("login")){
					this.rec_login();
				}
				if(reci[0].equals("friends")&&reci[1].equals("search")){
					this.rec_searchfriends();
				}
				if(reci[0].equals("friends")&&reci[1].equals("add")){
					this.rec_addfriends();
				}
				if(reci[0].equals("friendslist")){
					this.friendsList();
				}
				if(reci[0].equals("news")){
					this.newsList();
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			return -1;
			}
	}
	public void logoff(){
		String msg="logout";
		try{     
			out.println(msg);
		}catch(Exception e){
			e.printStackTrace();
			//return -1;
		}finally{
			try{
				out.close();
				in.close();
				socket.close();
			}catch(Exception e){e.printStackTrace();}
		}
	}
	public int login(String number,String password){
		String msg="login"+"|||"+number+"|||"+password+"\r";
		try{
		//socket = new Socket("192.168.7.13",10000);
		//dout= new OutputStreamWriter(socket.getOutputStream());
		//din= new InputStreamReader(socket.getInputStream());
		System.out.println(msg);    
		out.println(msg);   
		out.flush();
		//System.out.println(rec); 
		return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
		
	}
	public void rec_login(){
		System.out.println("im here");
		if(reci[1].equals("success")){
			num=reci[2];
			name=reci[3];
			major=reci[4];
			grade=reci[5];
			//return 0;
			login_state=1;
		}
		else{
			if(reci[1].equals("failure")&&reci[2].equals("1")){
				login_state=2;
				//return 1;
			}
			else
				login_state=3;
				//return 2;
		}
	}
	
	public int search_friends(String name){
		String msg="friends"+"|||"+"search"+"|||"+name+"\r";
		try{
			System.out.println(msg);
			out.println(msg);   
			
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public void rec_searchfriends(){
		if(reci[2]=="0"){
			//return 0;
			searchfriends_state=1;
		}
		else if(reci[2]=="1"){
			searchfriends_state=2;
			sfriends_number[sfriends_count]=reci[3];
			sfriends_name[sfriends_count]=reci[4];
			sfriends_major[sfriends_count]=reci[5];
			sfriends_grade[sfriends_count]=reci[6];
			sfriends_count++;
		}
		
	}
	public int addfriends(String number){
		String msg="friends"+"|||"+"add"+"|||"+num+"|||"+number+"|||"+"4"+"\r";
		try{
			System.out.println(msg);   
			out.println(msg);   
			
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public int agreefriends(String number){
		String msg="friends"+"|||"+"add"+"|||"+num+"|||"+number+"|||"+"2"+"\r";
		try{
			System.out.println(msg);
			out.println(msg);   
			
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public int disagreefriends(String number){
		String msg="friends"+"|||"+"add"+"|||"+num+"|||"+number+"|||"+"1"+"\r";
		try{
			System.out.println(msg);  
			out.println(msg);   
			
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public void rec_addfriends(){
		if(reci[2]=="1"){
			addfriends_state[addfriends_count]=1;
		}
		if(reci[2]=="2"){
			addfriends_state[addfriends_count]=2;
		}
		if(reci[2]=="3"){
			addfriends_state[addfriends_count]=3;
		}
		if(reci[2]=="4"){
			addfriends_state[addfriends_count]=4;
		}
		afriends_number[addfriends_count]=reci[3];
		afriends_name[addfriends_count]=reci[4];
		afriends_major[addfriends_count]=reci[5];
		afriends_grade[addfriends_count]=reci[6];
		addfriends_count++;
	}
	public int get_friendsList(){
		String msg="getfriends";
		try{
			out.println(msg);
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public void friendsList(){
		myfriends_id[myfriends_count]=reci[1];
		myfriends_name[myfriends_count]=reci[2];
		myfriends_major[myfriends_count]=reci[3];
		myfriends_grade[myfriends_count]=reci[4];
		myfriends_count++;
	}
	public int delfriends(String num,String hostnum){
		String msg="friends"+"|||"+"delete"+"|||"+num+hostnum+"\r";
		try{ 
			out.println(msg);
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public void newsList(){
		newsid[newscount]=reci[1];
		newstitle[newscount]=reci[2];
		newscount++;
	}
	public int getnews(String newspage){
		String msg="get10news"+"|||"+newspage+"|||"+"5"+"\r";
		try{   
			out.println(msg);
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public int getnewsbyid(String newsid){
		String msg="getnewsbyid"+"|||"+newsid+"|||"+"5"+"\r";
		try{
			out.println(msg);
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public int getnewscontent(String newsid){
		String msg="getnewscontent"+"|||"+newsid+"|||"+"5"+"\r";
		try{
			out.println(msg);
			return 0;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public void contentview(){
		
	}
}
